/**
 * Copyright © 2022 易久批信息技术有限公司. All rights reserved.
 */
package com.dysm.multithread;

/**
 * 测试threadLocal线程之间传递
 * @author xiay
 * @since 2022/4/17 3:44 下午
 */
public class ThreadLocalDemo {

    public static void main(String[] args) {
        // 直接使用ThreadLocal，子线程无法获取父线程的本地变量
        ThreadLocal<String> threadLocal = new ThreadLocal<>();
        // 使用InheritableThreadLocal，子线程可以获取父线程的本地变量
        ThreadLocal<String> threadLocal1 = new InheritableThreadLocal<>();
        threadLocal1.set("123");
        new Thread(() -> {
            String s = threadLocal1.get();
            System.out.println(Thread.currentThread().getName() + "获取线程本地变量：" + s);
        },"子线程1").start();
        System.out.println(Thread.currentThread().getName() + "获取线程本地变量：" + threadLocal1.get());

    }
}
